home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / UTILITY1 / MSWSRC35.ZIP / METER / METER.C < prev    next >
C/C++ Source or Header  |  1992-06-11  |  6KB  |  195 lines

  1. /*****************************************************************
  2. Module name: Meter.C
  3. Programmer : Jeffrey M. Richter.
  4. *****************************************************************/
  5.  
  6. #include <windows.h>
  7. #include <windowsx.h>
  8.  
  9. #include "meter.h"
  10.  
  11. extern const HINSTANCE _cdecl _hInstance;
  12.  
  13. char _szControlName[] = "Meter";
  14.  
  15. #define CBWNDEXTRA         (6)
  16. #define GWW_PARTSINJOB     (0)
  17. #define GWW_PARTSCOMPLETE  (2)
  18. #define GWW_FONT           (4)
  19.  
  20. ATOM NEAR RegisterControlClass (HINSTANCE hInstance);
  21. LRESULT CALLBACK MeterWndFn
  22.          (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  23.  
  24. /******** Dynamic-Link Library Initialization Routines **********/
  25.  
  26. #pragma argsused
  27. BOOL WINAPI LibMain (HINSTANCE hInstance, WORD wDataSeg,
  28.    WORD wHeapSize, LPSTR lpszCmdLine) {
  29.    BOOL fOk;
  30.    if (wHeapSize != 0) UnlockData(0);  // Let data segment move
  31.    fOk = (RegisterControlClass(hInstance) != NULL);
  32.    return(fOk);   // return TRUE if initialization is successful
  33. }
  34.  
  35. int WINAPI WEP (int nSystemExit) {
  36.    switch (nSystemExit) {
  37.       case WEP_SYSTEM_EXIT:   // System is shutting down.
  38.          break;
  39.  
  40.       case WEP_FREE_DLL:      // Usage count is zero (0).
  41.          break;
  42.    }
  43.    UnregisterClass(_szControlName, _hInstance);
  44.    return(1);                 // WEP function successful.
  45. }
  46.  
  47. ATOM NEAR RegisterControlClass (HINSTANCE hInstance) {
  48.    WNDCLASS wc;
  49.    wc.style         = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW;
  50.    wc.lpfnWndProc   = MeterWndFn;
  51.    wc.cbClsExtra    = 0;
  52.    wc.cbWndExtra    = CBWNDEXTRA;
  53.    wc.hInstance     = hInstance;
  54.    wc.hIcon         = (HICON) NULL;
  55.    wc.hCursor       = LoadCursor((HCURSOR) NULL, IDC_ARROW);
  56.    wc.hbrBackground = (HBRUSH) NULL;
  57.    wc.lpszMenuName  = NULL;
  58.    wc.lpszClassName = _szControlName;
  59.    return(RegisterClass(&wc));
  60. }
  61.  
  62. LRESULT CALLBACK MeterWndFn
  63.        (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  64.    LRESULT lResult = 0;
  65.    char szPercentage[10];
  66.    RECT rcClient, rcPrcnt;
  67.    PAINTSTRUCT ps;
  68.    WORD wPartsInJob, wPartsComplete;
  69.    HBRUSH hBrush;
  70.    DWORD dwColor;
  71.    HFONT hFont;
  72.  
  73.    switch (uMsg) {
  74.       case WM_GETDLGCODE:
  75.          lResult = DLGC_STATIC;
  76.          break;
  77.  
  78.       case WM_SETFONT:
  79.          SetWindowWord(hWnd, GWW_FONT, (HFONT) wParam);
  80.          if (lParam) // Redraw the control immediately
  81.             InvalidateRect(hWnd, NULL, FALSE);
  82.  
  83.          break;
  84.  
  85.       case WM_GETFONT:
  86.          lResult = GetWindowWord(hWnd, GWW_FONT);
  87.          break;
  88.  
  89.       case WM_ENABLE:
  90.          InvalidateRect(hWnd, NULL, FALSE);
  91.          break;
  92.  
  93.       case WM_CREATE:   // lParam == &CreateStruct
  94.          SendMessage(hWnd, MM_SETPARTSINJOB, 100, 0l);
  95.          SendMessage(hWnd, MM_SETPARTSCOMPLETE, 50, 0);
  96.          break;
  97.  
  98.       case WM_PAINT:
  99.          wPartsInJob = (WORD)
  100.             SendMessage(hWnd, MM_GETPARTSINJOB,  0, 0l);
  101.          wPartsComplete = (WORD)
  102.             SendMessage(hWnd, MM_GETPARTSCOMPLETE, 0, 0l);
  103.  
  104.          if (wPartsInJob == 0) {
  105.             wPartsInJob = 1;
  106.             wPartsComplete = 0;
  107.          }
  108.  
  109.          wsprintf(szPercentage, "%d%%",
  110.             (100 * wPartsComplete) / wPartsInJob);
  111.  
  112.          BeginPaint(hWnd, &ps);
  113.          hFont = GetWindowFont(hWnd);
  114.          if (hFont != NULL)
  115.             SelectObject(ps.hdc, hFont);
  116.  
  117.          // Set-up default foreground and background text colors.
  118.          SetBkColor(ps.hdc, GetSysColor(COLOR_WINDOW));
  119.          SetTextColor(ps.hdc, GetSysColor(COLOR_WINDOWTEXT));
  120.  
  121.          // Send WM_CTLCOLOR message to parent in case parent
  122.          // want touse a different color in the Meter control.
  123.          hBrush = FORWARD_WM_CTLCOLOR(GetParent(hWnd),
  124.             ps.hdc, hWnd, CTLCOLOR_METER, SendMessage);
  125.  
  126.          // Always use brush returned by parent.
  127.          SelectObject(ps.hdc, hBrush);
  128.  
  129.          // Invert the foreground and background colors.
  130.          dwColor = GetBkColor(ps.hdc);
  131.          SetBkColor(ps.hdc, SetTextColor(ps.hdc, dwColor));
  132.  
  133.          // Set rectangle coordinates to include only left
  134.          // percentage of the window.
  135.          GetClientRect(hWnd, &rcClient);
  136.          SetRect(&rcPrcnt, 0, 0,
  137.             (rcClient.right * wPartsComplete) / wPartsInJob,
  138.             rcClient.bottom);
  139.  
  140.          // Output the percentage value in the window.
  141.          // Function also paints left part of window.
  142.          SetTextAlign(ps.hdc, TA_CENTER | TA_TOP);
  143.          ExtTextOut(ps.hdc, rcClient.right / 2,
  144.             (rcClient.bottom -
  145.             HIWORD(GetTextExtent(ps.hdc, "X", 1))) / 2,
  146.             ETO_OPAQUE | ETO_CLIPPED, &rcPrcnt,
  147.             szPercentage, lstrlen(szPercentage), NULL);
  148.  
  149.          // Adjust rectangle so that it includes the remaining
  150.          // percentage of the window.
  151.          rcPrcnt.left = rcPrcnt.right;
  152.          rcPrcnt.right = rcClient.right;
  153.  
  154.          // Invert the foreground and background colors.
  155.          dwColor = GetBkColor(ps.hdc);
  156.          SetBkColor(ps.hdc, SetTextColor(ps.hdc, dwColor));
  157.  
  158.          // Output the percentage value a second time.
  159.          // Function also paints right part of window.
  160.          ExtTextOut(ps.hdc, rcClient.right / 2,
  161.             (rcClient.bottom -
  162.             HIWORD(GetTextExtent(ps.hdc, "X", 1))) / 2,
  163.             ETO_OPAQUE | ETO_CLIPPED, &rcPrcnt,
  164.             szPercentage, lstrlen(szPercentage), NULL);
  165.  
  166.          EndPaint(hWnd, &ps);
  167.          break;
  168.  
  169.       case MM_SETPARTSINJOB:
  170.          SetWindowWord(hWnd, GWW_PARTSINJOB, wParam);
  171.          InvalidateRect(hWnd, NULL, FALSE);
  172.          UpdateWindow(hWnd);
  173.          break;
  174.  
  175.       case MM_GETPARTSINJOB:
  176.          lResult = (LONG) GetWindowWord(hWnd, GWW_PARTSINJOB);
  177.          break;
  178.  
  179.       case MM_SETPARTSCOMPLETE:
  180.          SetWindowWord(hWnd, GWW_PARTSCOMPLETE, wParam);
  181.          InvalidateRect(hWnd, NULL, FALSE);
  182.          UpdateWindow(hWnd);
  183.          break;
  184.  
  185.       case MM_GETPARTSCOMPLETE:
  186.          lResult = (LONG) GetWindowWord(hWnd, GWW_PARTSCOMPLETE);
  187.          break;
  188.  
  189.       default:
  190.          lResult = DefWindowProc(hWnd, uMsg, wParam, lParam);
  191.          break;
  192.    }
  193.    return(lResult);
  194. }
  195.